package top;

/**
 *
 * 寻找重复数： 类似环形链表，寻找环的入口
 *
 * @author chenyw
 * @date 2022/7/20 11:12
 */
public class Top287findDuplicate {
    public int findDuplicate(int[] nums) {
        int fast = 0;
        int slow = 0;
        while (true) {
            fast = nums[nums[fast]];
            slow = nums[slow];
            if (slow == fast) {
                fast = 0;
                //环形的入口判断为 nums[slow] != nums[fast]
                while (nums[slow] != nums[fast]) {
                    slow = nums[slow];
                    fast = nums[fast];
                }
                break;
            }
        }
        return nums[slow];
    }
}
